<%@page import="deptentity.DeptComponent"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="empsession.EmpSessionHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="empsession.EmpSessionComponent"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%!    private EmpSessionComponent empSession = null;
    private String ejbsessionname = "ejb/EmpSessionBean";

    public void jspInit() {
        try {
            InitialContext context = new InitialContext();
            EmpSessionHome empSessionHome = (EmpSessionHome) context.lookup(ejbsessionname);
            empSession = empSessionHome.create();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
%>
<%
    String deptno = (String) request.getParameter("id");
    DeptComponent dept = empSession.getDeptByDeptno(new BigDecimal(deptno));
    boolean flag = true;
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="styles.css"/>
        <title>Отдел</title>
    </head>
    <body>
        <div class="navigation">
            <%@include file="navigation.jspf"%>
        </div><br/>
        <h1>Редактирование записи</h1>
        <br/>
        <form action="editDept.jsp">
            <table>
                <tr>
                    <th colspan="2">Отдел</th>
                </tr>
                <tr>
                    <th>Название*</th>
                    <td><input type="text" name="dname" value="<%= request.getParameter("dname") != null ? request.getParameter("dname") : dept.getDname()%>"></td>
                        <% if (request.getParameter("dname") != null && request.getParameter("dname").contentEquals("") && request.getParameter("edit") != null) {%>
                    <td class="restricting">Укажите название отдела!</td>
                    <% flag = false;
                        }%>
                </tr>
                <tr>
                    <th>Расположение*</th>
                    <td><input type="text" name="loc" value="<%= request.getParameter("loc") != null ? request.getParameter("loc") : dept.getLoc()%>"></td>
                        <% if (request.getParameter("loc") != null && request.getParameter("loc").contentEquals("") && request.getParameter("edit") != null) {%>
                    <td class="restricting">Укажите расположение отдела!</td>
                    <% flag = false;
                        }%>
                </tr>
            </table>
            <div class="comment">поля, помеченные звёздочкой, обязательны для заполнения</div><br/>
            <input type="submit" name="edit" value="Изменить">
            <input type="submit" name="cancel" value="Отмена">
            <input type="hidden" name="id" value="<%= dept.getDeptno()%>">
        </form>
        <%
            if (request.getParameter("edit") != null && flag) {
                dept.setDname(request.getParameter("dname"));
                dept.setLoc(request.getParameter("loc"));
                session.setAttribute("log", new extras.Log(true, "", "Запись успешно изменена!"));
                response.sendRedirect("viewDept.jsp?id=" + dept.getDeptno());
            }
            if (request.getParameter("cancel") != null) {
                session.setAttribute("log", new extras.Log(false, "", "Изменение записи отклонено!"));
                response.sendRedirect("viewDept.jsp?id=" + dept.getDeptno());
            }
        %>
    </body>
</html>
